package solution;

import java.util.HashSet;
import java.util.Set;

/**
 * DirectedDFS.java
 * ---------------------------------------
 */
public class DirectedDFS<V> {

    private Set<V> marked;

    public DirectedDFS(Graph<V> graph, V s) {
        this.marked = new HashSet<>();
        dfs(graph, s);
    }

    public DirectedDFS(Graph<V> graph, Iterable<V> sources) {
        this.marked = new HashSet<>();
        for (V s : sources) {
            if (!marked.contains(s)) {
                dfs(graph, s);
            }
        }
    }

    private void dfs(Graph<V> graph, V v) {
        marked.add(v);
        for (Edge<V> edge : graph.neighbors(v)) {
            V w = edge.getTo();
            if (!marked.contains(w)) {
                marked.add(w);
                dfs(graph, w);
            }
        }
    }

    public boolean marked(V v){
        return marked.contains(v);
    }
}
